<?php

namespace app\modules\weixin\controllers\common;

use Yii;
use yii\filters\Cors;
use yii\helpers\ArrayHelper;
use yii\web\Controller;

class BaseController extends Controller
{
    public $enableCsrfValidation = false;

    public function __construct($id, $module, array $config = [])
    {
        parent::__construct($id, $module, $config);
    }

    public function behaviors()
    {
        return ArrayHelper::merge([
            [
                'class' => Cors::className(),
                'cors' => [
                    'Origin' => ['*'],
                    'Access-Control-Request-Method' => ['GET', 'POST'],
                    'Access-Control-Allow-Credentials' => true,
                ],
            ],
        ], parent::behaviors());
    }

    public function beforeAction($action)
    {
        $allow_route = [
            '/weixin/common/login',
            '/weixin/common/logout',
            '/weixin/common/user-login',
            '/weixin/common/user-logout',
            '/weixin/common/user-register',
            '/weixin/common/forget-pwd',
            '/weixin/common/get-modify-code',
            '/weixin/common/user-login',
            '/weixin/common/user-register',
            '/weixin/common/user-validate-code',
            '/weixin/common/user-logout',
            '/weixin/common/user-forget-pwd',
        ];
        if (parent::beforeAction($action)) {
//            $origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
            //            $allow_origin = [
            //                'http://10.9.87.101:3000',
            //                'http://localhost:3000'
            //            ];
            //            if (in_array($origin, $allow_origin)) {
            //                header('Access-Control-Allow-Origin:' . $origin);
            //                header('Access-Control-Allow-Methods:POST,GET');
            //                header('Access-Control-Allow-Credentials:true');
            //            }
            //            $route = "/weixin/{$action->controller->id}/{$action->id}";
            //            if (in_array($route, $allow_route) == false) {
            //                Yii::$app->response->format = Response::FORMAT_JSON;
            //                if (!$this->checkLogin()) {
            //                    Yii::$app->response->data = [
            //                        'code' => -1,
            //                        'message' => '请先登录',
            //                        'url' => '/weixin/common/login'
            //                    ];
            //                    return false;
            //                }
            //            }
            return true;
        } else {
            return false;
        }
    }

    protected function checkLogin()
    {
        $session_member = Yii::$app->session->get('member_info');
        $session_user = Yii::$app->session->get('user_info');
        if (empty($session_member) && empty($session_user)) {
            return false;
        }
        return true;
    }
}